<template>
  <div class="login-container">
    <el-form ref="myform" class="myfrom" :rules="rules" :model="form">
      <h3>iHRM后台登录系统</h3>
      <el-form-item prop="mobile">
        <el-input
          prefix-icon="el-icon-user-solid"
          placeholder="请输入登录账号"
          v-model="form.mobile"
        ></el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input
          prefix-icon="el-icon-s-tools"
          show-password
          placeholder="请输入登录密码"
          v-model="form.password"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button class="btn" @click="login" :loading="loading"
          >登录</el-button
        >
      </el-form-item>
      <div class="mark">还没有账号？立即注册</div>
    </el-form>
  </div>
</template>

<script>
// import {loginApi} from '@/api/user.js'
import { mapActions } from 'vuex'
export default {
  name: 'Login',
  data () {
    return {
      form: {
        mobile: '13800000002',
        password: '123456'
      },
      rules: {
        mobile: [
          { required: true, message: '请输入登录账号', trigger: 'blur' },
          {
            validator: (rule, value, callback) => {
              // rule: 规则
              // value：当前验证项的值
              // callback：
              //	如果验证成功 callback()
              //  如果验证失败 callback(new Error(错误信息))
              var reg = /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/
              //测试正则
              reg.test(value) ? callback() : callback('请输入正确的登录账号')
            }
          }
        ],
        password: [
          { required: true, message: '请输入登录密码', trigger: 'blur' },
          { min: 6, max: 8, message: '密码长度为6~8个字符', trigger: 'blur' }
        ]
      },
      loading: false
    }
  },
  methods: {
    ...mapActions({
      userlogin: 'user/login'
    }),
    login () {
      this.$refs.myform.validate(async valid => {
        if (valid) {
          this.loading = true
          try {
            await this.userlogin(this.form)
            //判断是否携带参数
            const _redirect=this.$route.query._redirect
            if(_redirect){
              this.$router.push(_redirect)
            }else{
              this.$router.push('/')
            }
            // const res=await loginApi(this.form)
            //添加登录成功提示信息
            this.$message.success('欢迎主人回家')
          } catch (error) {
            console.log(error.message)
            //finally:无论是否成功都执行
          } finally {
            this.loading = false
          }
        } else {
          console.log('验证失败')
        }
      })
    }
  }
}
</script>

<style lang="scss">
.login-container {
  height: 100%;
  background-image: url('~@/assets/common/wallhaven-j3kpj5.jpg');
  // background-image: url('~@/assets/common/mm.jpg') ;
  background-size: 100% 100%;
  background-position: center;
  .myfrom {
    width: 520px;
    height: 400px;
    padding: 35px 35px 15px 35px;
    position: fixed;
    top: 44%;
    left: 50%;
    transform: translate(-50%, -50%);
    h3 {
      font-size: 45px;
      font-weight: 100;
      color: #fff;
      text-align: center;
      letter-spacing: 6px;
      margin-top: 0;
    }
    .btn {
      width: 100%;
      height: 60px;
      background: #407ffe;
      border: none;
      color: #fff;
      font-size: 24px;
    }
    input.el-input__inner {
      height: 50px;
      background-color: #d3e4ff;
      color: #68bbfe;
    }
    .mark {
      color: #fff;
      text-align: center;
      line-height: 50px;
    }
  }
}
</style>
